{include file="public/header" /}
</head>
<body>
<div class="wrap js-check-wrap">
    <ul class="nav nav-tabs">
        <li><a href="{:url('authMember/index')}">后台用户</a></li>
        <li class="active"><a href="javascript:;">添加用户</a></li>
    </ul>
    <form method="post" class="form-horizontal js-ajax-form margin-top-20">
        <table class="table table-bordered">
            <tr>
                <th width="150">用户名<span class="form-required">*</span></th>
                <td>
                    <input class="form-control" type="text" name="username" value="" placeholder="请输入3-10位字母组成的用户名"/>
                </td>
            </tr>
            <tr>
                <th>密码<span class="form-required">*</span></th>
                <td>
                    <input class="form-control" type="text" name="password" value="" placeholder="请输入6-16位字母或数字组成的密码"/>
                    <br/>
                    <button type="button" class="btn btn-primary js-random-password">随机32位密码</button>
                </td>
            </tr>
            <tr>
                <th>状态</th>
                <td>
                    <select class="form-control" name="status" style="width: 200px;">
                        <option value='1'>启用</option>
                        <option value='0'>禁用</option>
                    </select>
                </td>
            </tr>
            <tr>
                <th>所属权限组</th>
                <td>
                    {foreach name="groupList" item="vo"}
                        <input type='radio' name='groupId' {if condition="$key eq 1"}checked{/if} value='{$vo.id}'/>{$vo.name}　
                    {/foreach}
                </td>
            </tr>
            <tr>
                <th>描述</th>
                <td>
                    <textarea  class="form-control" name="remark"  placeholder="请填写描述"></textarea>
                </td>
            </tr>
            <tr>
                <th></th>
                <td>
                    <div class="col-sm-offset-2 col-sm-10">
                        <input type="hidden" name="action" value="{$params['action']}" />
                        <button type="submit" class="btn btn-primary js-ajax-submit">提交</button>
                        <a class="btn btn-default" href="{:url('authMember/index')}">返回</a>
                    </div>
                </td>
            </tr>
        </table>
    </form>
</div>
<script src="{$static}js/layer/layer.js"></script>
<script type="text/javascript">
    $(".js-ajax-submit").click(function () {
        var username = $("input[name=username]").val();
        var password = $("input[name=password]").val();
        if(username === "" || username === undefined) {
            layer.alert('请输入用户名', {title: '提示', icon: 2});
            return false;
        }
        if(password === "" || password === undefined) {
            layer.alert('请输入密码', {title: '提示', icon: 2});
            return false;
        }

        $.ajax({
            url: "{:url('authMember/operateAjax')}",
            type: "POST",
            data: $(".js-ajax-form").serialize(),
            dataType: 'json',
            async: false,
            success: function (res) {
                if(res.errorCode === 200){
                    layer.confirm(res.message, {
                        btn:['确认','取消'],
                        success:function(){
                            this.enterEsc = function (event) {
                                if (event.keyCode === 13) {
                                    $(".layui-layer-btn0").click();
                                    return false;
                                }
                            };
                            $(document).on('keydown', this.enterEsc);

                            $(".layui-layer-btn0").on("click",function() {
                                window.location.href = "{:url('authMember/index')}";
                                return false
                            })
                        },
                        end:function(){
                            $(document).off('keydown',this.enterEsc);
                        }
                    });
                }else{
                    layer.alert(res.message, {
                        title: '提示',
                        icon: 2
                    });
                }
            }
        });
        return false;
    });

    $(".js-random-password").click(function () {
        $.ajax({
            url: "{:url('AuthMember/getRandomPassword')}",
            type: "POST",
            data: {length: 32},
            dataType: 'json',
            async: false,
            success: function (res) {
                $('input[name=password]').val(res.data.password);
            }
        });
    });
</script>
</body>
</html>
